package com.zan.controller;

import com.zan.entity.Users;
import com.zan.service.UserService;
import com.zan.vo.ResStatus;
import com.zan.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/user")
@Api(value = "提供用户业务相关接口", tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "username", value = "用户登录账号", required = true),
            @ApiImplicitParam(dataType = "String", name = "password", value = "用户登录密码", required = true)
    })
    @GetMapping("/login")
    public ResultVo login(String username, String password) {
        logger.info("登录成功");
        ResultVo resultVo = userService.checkLogin(username, password);
        return resultVo;
    }

    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    public ResultVo register(@RequestBody Users user) {
        return userService.userRegister(user.getUsername(), user.getPassword());
    }

    @ApiOperation("校验token是否过期")
    @GetMapping("/checkToken")
    public ResultVo userTokenCheck(@RequestHeader("token") String token) {
        return new ResultVo(ResStatus.OK.code, "success", null);
    }
}
